﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public partial class CopySubject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int SubID = HelperURL.GetIntFromUrlString("ID");
                KP_Subject subject = new KP_Subject();
                if (!subject.LoadByPrimaryKey(SubID))
                {
                    HelperMessage.MessageUrl("招标项目无效！", "../Appraisal/SubjectList.aspx");
                    return;
                }
                else
                {
                    txtName.Text = "新建 " + subject.SubjectName;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int SubID = HelperURL.GetIntFromUrlString("ID");
            KP_Subject subject = new KP_Subject();
            subject.LoadByPrimaryKey(SubID);

            KP_Subject newsubject = new KP_Subject();
            KP_Subject_Employee new_se = new KP_Subject_Employee();
            KP_Subject_Table new_st = new KP_Subject_Table();
            KP_Subject_Table_Ump new_stu = new KP_Subject_Table_Ump();

            CopyNewSubject(subject, newsubject);
            CopyEmployee(SubID , newsubject.ID ,new_se);
            CopySubjectTable(SubID , newsubject.ID ,new_st);
            CopySubjectUmp(SubID , newsubject.ID ,new_stu);

            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                newsubject.Save();
                new_se.Save();
                new_st.Save();
                new_stu.Save();
                tx.CommitTransaction();
                HelperMessage.MessageUrl("复制完成！", "../Appraisal/SubjectList.aspx");
            }
            catch
            {
                tx.RollbackTransaction();
                TransactionMgr.ThreadTransactionMgrReset();
            }

        }

        private void CopySubjectUmp(int oldSubID ,int newSubID ,KP_Subject_Table_Ump new_stu)
        {
            KP_Subject_Table_Ump st = new KP_Subject_Table_Ump();
            st.Where.SubjectID.Value = oldSubID;
            st.Query.Load();
            while (!st.EOF)
            {
                new_stu.AddNew();
                new_stu.SubjectID = newSubID;
                new_stu.KpTableID = st.KpTableID;
                new_stu.UmpID = st.UmpID;
                new_stu.Status = st.Status;
                new_stu.UmpType = st.UmpType;

                st.MoveNext();
            }
        }

        private void CopySubjectTable(int oldSubID ,int newSubID ,KP_Subject_Table new_st)
        {
            KP_Subject_Table st = new KP_Subject_Table();
            st.Where.SID.Value = oldSubID;
            st.Query.Load();
            while (!st.EOF)
            {
                new_st.AddNew();
                new_st.SID = newSubID;
                new_st.KpID = st.KpID;
                new_st.Rate = st.Rate;
                new_st.MinScore = st.MinScore;
                new_st.MaxScore = st.MaxScore;
                new_st.CountMod = st.CountMod;

                st.MoveNext();
            }
        }

        private void CopyEmployee(int oldSubID ,int newSubID ,KP_Subject_Employee new_se)
        {
            KP_Subject_Employee se = new KP_Subject_Employee();
            se.Where.SID.Value = oldSubID;
            se.Query.Load();
            while (!se.EOF)
            {
                new_se.AddNew();
                new_se.SID = newSubID;
                new_se.EmployeeID = se.EmployeeID;
                new_se.Total = 0;
                new_se.OrderID = se.OrderID;

                se.MoveNext();
            }
        }

        private void CopyNewSubject(KP_Subject old, KP_Subject newsubject)
        {
            //新建项目
            newsubject.AddNew();
            //WorkController w = new WorkController();
            //string sHead = old.CodeHead;
            //string sYear = DateTime.Now.Year.ToString();
            //sHead = sHead + "-" + sYear + "-";
            //newsubject.Code = w.p_GetMaxSubjectCode(sHead);

            newsubject.Code = old.Code;
            newsubject.ID = SequenceGenerator.GetNextVal("SubjectID");
            newsubject.Creater = DBController.GetInstance().GetAuthenticatedUserID();
            newsubject.CreateDate = DateTime.Now;
            newsubject.SubjectName = txtName.Text;

            newsubject.BeginDate = old.BeginDate;
            newsubject.EndDate = old.EndDate;
            newsubject.SubjectDesc = old.SubjectDesc;
            newsubject.ReMark = old.ReMark;
            newsubject.Enable = "-";// chkEnable.Checked ? "Y" : "N";
            newsubject.Bidder = old.Bidder;
            //s.Bidder = txtBidder.Text ;
            newsubject.Super = old.Super;
            //newsubject.Code = old.
            newsubject.Address = old.Address;
            newsubject.CodeHead = old.CodeHead;
            newsubject.ResultNum = old.ResultNum;
            newsubject.PayType = old.PayType;
            newsubject.BidDesc = old.BidDesc;
        }

    }
}
